package com.fjnu.controller;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.log4j.LogManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fjnu.controller.common.BaseController;
import com.fjnu.dao.common.DaoException;
import com.fjnu.entities.MainInstrument;
import com.fjnu.entities.User;
import com.fjnu.service.IUserService;
import com.fjnu.service.InstrumentService;
import com.fjnu.service.impl.UserService;
/**
 * 实现主机表的增删改查的控制层
 * @author yangzeyi
 *
 */

@Controller
public class InstrumentController extends BaseController{
	 

	private static Logger logger =LogManager.getLogger(InstrumentController.class);



	@Autowired	
	private InstrumentService instrumentservice;
	@Autowired
	private IUserService userService;
	/**
	 * 实现单个资产入库
	 * @param maininstrument
	 * @return
	 */
	@RequestMapping(value="/toAddIns")
	public Map<String,Object> toAddIns(@RequestBody MainInstrument maininstrument){
		logger.info("新增主机设备："+maininstrument.getInstrumentName());
		if (!init(maininstrument).get("status").equals(SUCCESS))
			return getData2Result();
		Subject currentUser = SecurityUtils.getSubject();  
		 Session session = currentUser.getSession();
		User inputPerson=(User) session.getAttribute("subject");
		maininstrument.setInputPerson(inputPerson);
		maininstrument.setStatusQuo(maininstrument.GOOD);
		Date inputTime=new Date();
		maininstrument.setInputTime(inputTime);
		instrumentservice.addInstrument(maininstrument);
		//Map<String,Object> map = new HashMap<String,Object>(1);
		
		//map.put("success", true);
		return getData2Result();
	}
	
	@RequiresRoles("后勤管理员")
	@ResponseBody
	@RequestMapping(value="/queryList")
	public Map<String,Object> queryInstrumentList(@RequestParam(value="pageIndex",required=false) Integer pageIndex,
			@RequestParam(value="pageSize",required=false) Integer pageSize,
			@RequestParam(value="condtion",required=false) String condition){
		logger.info("查询主机设备列表 ");
		if (!init().get("status").equals(SUCCESS))
			return getData2Result();
		if(condition==null){
		 condition="";
		}
		if(pageSize==null){
			pageSize=10;
		}
		if(pageIndex==null){
			pageIndex=1;
		}
		List<MainInstrument> list = instrumentservice.findManyInstrument(condition,pageSize,pageIndex);
		//Map<String,Object> map = new HashMap<String,Object>();
		//map.put("data", list);
		//map.put("success",true);
		if(list.size()==0){
			throw new DaoException("主机信息为空");
		}
		put("data",list);
		return getData2Result();
	}
	@RequiresRoles("普通教职工")
	@ResponseBody
	@RequestMapping(value="/findManyByUnit")
	public Map<String,Object> findManyByUnit(){
		if (!init().get(STATUS).equals(SUCCESS))
			return getData2Result();
		Subject currentUser = SecurityUtils.getSubject();  
		Session session = currentUser.getSession();
		User user1=(User) session.getAttribute("subject");
		User user2= userService.findUserUnique(user1.getId());
		List<MainInstrument> list = instrumentservice.findManyByUnit(user2);
		put("data",list);
		return getData2Result();
	}
	@ResponseBody
	@RequestMapping(value="/findRecipientsUnitNum")
	public List<MainInstrument> findRecipientsUnitNum(@RequestParam(value="pageIndex",required=false) Integer pageIndex,
			@RequestParam(value="pageSize",required=false) Integer pageSize){
		//logger.info("查询列表  日期："+new Date());
			if(pageSize==null){
				pageSize=10;
			}
			if(pageIndex==null){
				pageIndex=1;
			}
			return instrumentservice.findRecipientsUnitNum(pageIndex, pageSize);
	}
	
	@ResponseBody
	@RequestMapping(value="/findclassification")
	public List<MainInstrument> findclassification(@RequestParam(value="pageIndex",required=false) Integer pageIndex,
			@RequestParam(value="pageSize",required=false) Integer pageSize){
			if(pageSize==null){
				pageSize=10;
			}
			if(pageIndex==null){
				pageIndex=1;
			}
			return instrumentservice.findclassification(pageIndex, pageSize);
	}
	
	@ResponseBody
	@RequestMapping(value="/findInstrumentNum")
	public List<MainInstrument> findInstrumentNum(@RequestParam(value="pageIndex",required=false) Integer pageIndex,
			@RequestParam(value="pageSize",required=false) Integer pageSize){
			if(pageSize==null){
				pageSize=10;
			}
			if(pageIndex==null){
				pageIndex=1;
			}
			return instrumentservice.findInstrumentNum(pageIndex, pageSize);
	}
	/**
	 * 资产退库
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/delInstrument")//,method = RequestMethod.POST,consumes="application/json")
	public Map<String,Object> delInstrument(int id){
		//System.out.println("ddd"+id);
		if(!init(id).get(STATUS).equals(SUCCESS))
			return getData2Result();
		logger.info("删除设备ID为 "+id+" 的主机信息");
		instrumentservice.delInstrument(id);
		//Map<String,Object> map = new HashMap<String,Object>(1);
		//map.put("success", true);
		return getData2Result();
	}
	/**
	 * 对单个资产进行报废处理
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/ontTodead")
	public Map<String,Object> Onetodead(int id){
		if(!init(id).get(STATUS).equals(SUCCESS))
			return getData2Result();
		logger.info("单个资产报废");
		MainInstrument mainInstrument = new MainInstrument();
		mainInstrument = instrumentservice.findUniqueInstrument(id);
		mainInstrument.setStatusQuo(mainInstrument.DEAD);
		instrumentservice.updateInstrument(mainInstrument);
		return getData2Result();
	}
	/**
	 * 对设备进行一般修改
	 * @param mi
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/updateInstrument")
	public Map<String,Object> updateInstrument(/*@RequestParam(value="id") int id,
			@RequestParam(value="instrumentName",required=false) String instrumentName,
			@RequestParam(value="storageAreaName",required=false) String storageAreaName,
			@RequestParam(value="statusQuo",required=false) String statusQuo,
			@RequestParam(value="remark",required=false) String remark*/
			@RequestBody MainInstrument mi){
		/*
		mainInstrument mi= new mainInstrument();
		mi.setId(id);
		mi.setInstrumentName(instrumentName);
		mi.setStorageAreaName(storageAreaName);
		mi.setStatusQuo(statusQuo);
		mi.setRemark(remark);
		*/
		logger.info("更新设备信息");
		if(!init(mi).get(STATUS).equals(SUCCESS))
			return getData2Result();
		instrumentservice.updateInstrument(mi);
		//Map<String,Object> map = new HashMap<String,Object>(1);
		//map.put("success", true);
		
		return getData2Result();		
	}
	@ResponseBody
	@RequestMapping(value="/updateRecipientsPerson")
	public Map<String,Object> updateRecipientsPerson(int id,String recipientsPerson){
		logger.info("修改领用人");
		if(!init(id,recipientsPerson).get(STATUS).equals(SUCCESS))
			return getData2Result();
		instrumentservice.updateRecipientsPerson(id, recipientsPerson);
		return getData2Result();
	}
}
